<template>
	<view class="favorites-list" :class="[config.style]">
		<!-- #ifdef H5-->
		<Top />
		<!-- #endif -->
		<!--数据列表部分-->
		<view class="list-section uni-clearfix">
			<!-- top="xxx"下拉布局往下偏移,防止被悬浮菜单遮住 -->
			<mescroll-uni ref="mescrollRef" @init="mescrollInit" :top="mescrollTop" :down="downOption" @down="downCallback" @up="upCallback">
				<!-- 数据列表 -->
				<view class="list">
					<view class="item" v-for="(item,index) in list" :key="index" @click="goToDetailLink(item.info_id)">
						<view class="cover">
							<image mode="widthFix" :src="item.info.cover_200" @error="imageError" class="image"></image>
						</view>
						<view class="info">
							<view class="title text-ellipsis-2">{{item.info.title}}</view>
							<view class="description text-ellipsis">{{item.info.description}}</view>
							<view class="price">￥ {{item.info.price}}</view>
						</view>
					</view>
				</view>
			</mescroll-uni>
		</view>
	</view>
</template>

<script>
	import { mapState,mapMutations } from 'vuex'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js"
	import Top from '../../common/top.vue'
	
	export default {
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		components: {
			Top
		},
		data() {
			return {
				urlParams: {}, //url传递过来的参数
				page: 1, //当前页码
				list: [], //列表数据
				mescrollTop: 0,
				downOption: {
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				upOption:{
					// page: {
					// 	size: 10 // 每页数据的数量
					// },
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty:{
						tip: '~ 搜索无数据 ~', // 提示
						btnText: '去看看'
					}
				},
			}
		},
		onLoad(option) {
			//获取url参数
			this.getUrlParam()
			// #ifdef H5
			this.mescrollTop = 70
			// #endif
		},
		created() {

		},
		computed: {
			...mapState(['config'])
		},
		methods: {
			//获取url参数
			getUrlParam: function(){
				let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
				let curRoute = routes[routes.length - 1].route //获取当前页面路由
				let curParam = routes[routes.length - 1].options; //获取路由参数
				this.urlParams = curParam
			},
			//上拉加载的回调
			upCallback: function() {
				let that = this

				let data = this.urlParams
					data.action = 'lists'
					data.page = this.page
				//获取首页数据
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/favorites'
					//#endif
					//#ifdef H5
					url = 'entry/site/favorites'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							let list = res.data.data.data
							that.list = that.list.concat(list);
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(list.length);
							that.page = that.page + 1
						}
					}
				})
			},
			//下拉刷新
			downCallback: function() {
				let that = this
				this.page = 1
				let data = this.urlParams
					data.action = 'lists'
					data.page = this.page
				//获取数据
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/favorites'
					//#endif
					//#ifdef H5
					url = 'entry/site/favorites'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							that.list = []
							that.list = res.data.data.data
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(that.list.length);
							that.page = 2
						}
					}
				})
			},
			
			goToDetailLink: function(id,type){
				let page = '../../goods/detail'
				let urlParams = 'id='+id
				let url = page + '?' + urlParams
				console.log(url)
				//指定跳转地址
				uni.navigateTo({
				    url: url
				});
			},
		}
	}
</script>

<style lang="scss">
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #f8f8f8;
		min-height: 100%;
		height: auto;
	}
	
	/*顶部固定部分*/
	.top-warp {
		background-color: #ffffff;
		.tabs-block {
			display: flex;
			flex-wrap: wrap;
			margin: 0 20rpx;
			.tabs {
				flex: 1;
			}
		}
	}
	.list {
		/*小图*/
		padding: 0 10rpx;
		.item {
			display: flex;
			box-sizing:border-box;
			background-color: #fff;
			margin: 20rpx 0;
			border-radius: 20rpx;
			box-shadow: 0 0 20rpx rgba(0,0,0,.02);
			overflow: hidden;
			
			.cover {
				position: relative;
				width: 240rpx;
				overflow: hidden;
				.image {
					border-radius: 10rpx 0 0 10rpx;
					width: 100%;
					height: auto;
					display: block;
				}
			}
			.info {
				flex: 1;
				padding: 10rpx;
				.title {
					font-size: 28rpx;
					color: #000;
					font-weight: 600;
					height: 72rpx;
					line-height: 36rpx;
				}
				.description {
					color: #999;
					font-size: 24rpx;
				}
				.price {
					margin-top: 40rpx;
					font-size: 24rpx;
					text-align: right;
				}
				.exp-info {
					margin-top: 10rpx;
					color: #999;
					font-size: 24rpx;
					.item {
						display: inline;
						margin: 0;
						.num {
							margin: 0 10rpx;
						}
					}
				}
			}
			
		}
	}
	
	.Blue {
		.price {
			color: $Blue;
		}
	}
	.Green {
		.price {
			color: $Green;
		}
	}
	.LightRed {
		.price {
			color: $LightRed;
		}
	}
	.Orange {
		.price {
			color: $Orange;
		}
	}
	.LightPink {
		.price {
			color: $LightPink;
		}
	}
	.Magenta {
		.price {
			color: $Magenta;
		}
	}
</style>
